package com.iuie.ns.system.maker;

import java.util.ArrayList;
import java.util.List;

import com.iuie.ns.frame.utils.StringUtils;

public abstract class AbstractMarkdownBatchMaker<T> extends AbstractBatchMaker<T> {
	
	public AbstractMarkdownBatchMaker(String rule) {
		super(rule);
	}

	@Override
	public List<T> make() {
		String[] arr = this.text.split("\n"); // 分成多行
		if (arr == null || arr.length == 0) {
			return null;
		}
		List<T> list = new ArrayList<>();
		for (String record: arr) {
			String[] items = StringUtils.split(record, "|");
			if (items == null || items.length == 0) {
				continue ;
			}
			
			T obj = getObject();
			// 处理一下数据，去掉前后空白
			List<String> mItems = this.dealItems(items);
			for (int i = 0; i < mItems.size(); i++) {
				setObjectAttribute(obj, mItems.get(i), i);
			}
			if (obj == null) {
				continue ;
			}
			list.add(obj);
		}
		return list;
	}

	protected List<String> dealItems(String[] items) {
		List<String> list = new ArrayList<>();
		for (int i = 0; i < items.length; i ++) {
			String str = items[i].replaceAll("[\\s\\u00A0]+$", ""); // 删除ASCII160空格
			str = StringUtils.deleteWhitespace(str);
			list.add(str);
		}
		return list;
	}
	
}
